package com.aphrodite.io.sip.call;

import com.aphrodite.io.sip.address.NameAddress;
import com.aphrodite.io.sip.dialog.ExtendedInviteDialog;
import com.aphrodite.io.sip.dialog.ExtendedInviteDialogListener;
import com.aphrodite.io.sip.dialog.InviteDialog;
import com.aphrodite.io.sip.header.StatusLine;
import com.aphrodite.io.sip.message.Message;
import com.aphrodite.io.sip.provider.SipProvider;

/**
 * Class ExtendedCall extends basic SIP calls.
 * <p>
 * It implements: <br>
 * - call transfer (REFER/NOTIFY methods)
 */
public class ExtendedCall extends Call implements ExtendedInviteDialogListener {

	/** Nonce for the next authentication. */
	String next_nonce;

	/** User's passwd. */
	String passwd;

	/** Qop for the next authentication. */
	String qop;

	/** User name. */
	String realm;

	Message refer;

	/** User name. */
	String username;

	ExtendedCallListener xcall_listener;

	/**
	 * Creates a new ExtendedCall for the already received INVITE request
	 * <i>invite</i>.
	 */
	public ExtendedCall(SipProvider sip_provider, Message invite, String contact_url, ExtendedCallListener call_listener) {
		super(sip_provider, (String) null, contact_url, call_listener);
		this.from_url = invite.getToHeader().getNameAddress().toString();
		this.dialog = new ExtendedInviteDialog(sip_provider, invite, this);
		this.remote_sdp = invite.getBody();

		this.xcall_listener = call_listener;
		this.refer = null;
		this.username = null;
		this.realm = null;
		this.passwd = null;
		this.next_nonce = null;
		this.qop = null;
	}

	/** Creates a new ExtendedCall specifing the sdp. */
	/*
	 * public ExtendedCall(SipProvider sip_provider, String from_url, String
	 * contact_url, String sdp, ExtendedCallListener call_listener) {
	 * super(sip_provider,from_url,contact_url,sdp,call_listener);
	 * xcall_listener=call_listener; }
	 */

	/**
	 * Creates a new ExtendedCall for the already received INVITE request
	 * <i>invite</i>.
	 */
	public ExtendedCall(SipProvider sip_provider, Message invite, String contact_url, String username, String realm,
			String passwd, ExtendedCallListener call_listener) {
		super(sip_provider, (String) null, contact_url, call_listener);
		this.from_url = invite.getToHeader().getNameAddress().toString();
		this.dialog = new ExtendedInviteDialog(sip_provider, invite, username, realm, passwd, this);
		this.remote_sdp = invite.getBody();

		this.xcall_listener = call_listener;
		this.refer = null;
		this.username = username;
		this.realm = realm;
		this.passwd = passwd;
		this.next_nonce = null;
		this.qop = null;
	}

	/** Creates a new ExtendedCall. */
	public ExtendedCall(SipProvider sip_provider, String from_url, String contact_url,
			ExtendedCallListener call_listener) {
		super(sip_provider, from_url, contact_url, call_listener);
		this.xcall_listener = call_listener;
		this.refer = null;
		this.username = null;
		this.realm = null;
		this.passwd = null;
		this.next_nonce = null;
		this.qop = null;
	}

	/** Creates a new ExtendedCall. */
	public ExtendedCall(SipProvider sip_provider, String from_url, String contact_url, String username, String realm,
			String passwd, ExtendedCallListener call_listener) {
		super(sip_provider, from_url, contact_url, call_listener);
		this.xcall_listener = call_listener;
		this.refer = null;
		this.username = username;
		this.realm = realm;
		this.passwd = passwd;
		this.next_nonce = null;
		this.qop = null;
	}

	/** Accepts a call transfer request */
	public void acceptTransfer() {
		((ExtendedInviteDialog) dialog).acceptRefer(refer);
	}

	/** Starts a new call with the <i>invite</i> message request */
	public void call(Message invite) {
		dialog = new ExtendedInviteDialog(sip_provider, this);
		local_sdp = invite.getBody();
		if (local_sdp != null)
			dialog.invite(invite);
		else
			dialog.inviteWithoutOffer(invite);
	}

	/** Starts a new call, inviting a remote user (<i>r_user</i>) */
	public void call(String r_user, String from, String contact, String sdp) {
		log.info(this, "calling " + r_user);
		if (username != null)
			dialog = new ExtendedInviteDialog(sip_provider, username, realm, passwd, this);
		else
			dialog = new ExtendedInviteDialog(sip_provider, this);
		if (from == null)
			from = from_url;
		if (contact == null)
			contact = contact_url;
		if (sdp != null)
			local_sdp = sdp;
		if (local_sdp != null)
			dialog.invite(r_user, from, contact, local_sdp);
		else
			dialog.inviteWithoutOffer(r_user, from, contact);
	}

	/** Waits for an incoming call */
	public void listen() {
		if (username != null)
			dialog = new ExtendedInviteDialog(sip_provider, username, realm, passwd, this);
		else
			dialog = new ExtendedInviteDialog(sip_provider, this);
		dialog.listen();
	}

	/** Notifies the satus of an other call */
	public void notify(int code, String reason) {
		((ExtendedInviteDialog) dialog).notify(code, reason);
	}

	/**
	 * When an incoming request is received within the dialog different from
	 * INVITE, CANCEL, ACK, BYE
	 */
	public void onDlgAltRequest(InviteDialog d, String method, String body, Message msg) {}

	/**
	 * When a response is received for a request within the dialog different
	 * from INVITE, CANCEL, ACK, BYE
	 */
	public void onDlgAltResponse(InviteDialog d, String method, int code, String reason, String body, Message msg) {}

	// ************** Inherited from InviteDialogListener **************

	/** When an incoming NOTIFY request is received within the dialog */
	public void onDlgNotify(InviteDialog d, String event, String sipfragment, Message msg) {
		if (d != dialog) {
			log.info(this, "NOT the current dialog");
			return;
		}
		log.info(this, "onDlgNotify()");
		if (event.equals("refer")) {
			Message fragment = new Message(sipfragment);
			log.info(this, "Notify: " + sipfragment);
			if (fragment.isResponse()) {
				StatusLine status_line = fragment.getStatusLine();
				int code = status_line.getCode();
				String reason = status_line.getReason();
				if (code >= 200 && code < 300) {
					log.info(this, "Call successfully transferred");
					if (xcall_listener != null)
						xcall_listener.onCallTransferSuccess(this, msg);
				} else if (code >= 300) {
					log.info(this, "Call NOT transferred");
					if (xcall_listener != null)
						xcall_listener.onCallTransferFailure(this, reason, msg);
				}
			}
		}
	}

	/** When an incoming REFER request is received within the dialog */
	public void onDlgRefer(InviteDialog d, NameAddress refer_to, NameAddress referred_by, Message msg) {
		if (d != dialog) {
			log.info(this, "NOT the current dialog");
			return;
		}
		log.info(this, "onDlgRefer(" + refer_to.toString() + ")");
		refer = msg;
		if (xcall_listener != null)
			xcall_listener.onCallTransfer(this, refer_to, referred_by, msg);
	}

	/** When a response is received for a REFER request within the dialog */
	public void onDlgReferResponse(InviteDialog d, int code, String reason, Message msg) {
		if (d != dialog) {
			log.info(this, "NOT the current dialog");
			return;
		}
		log.info(this, "onDlgReferResponse(" + code + " " + reason + ")");
		if (code >= 200 && code < 300) {
			if (xcall_listener != null)
				xcall_listener.onCallTransferAccepted(this, msg);
		} else if (code >= 300) {
			if (xcall_listener != null)
				xcall_listener.onCallTransferRefused(this, reason, msg);
		}
	}

	/** Refuses a call transfer request */
	public void refuseTransfer() {
		((ExtendedInviteDialog) dialog).refuseRefer(refer);
	}

	/** Requests a call transfer */
	public void transfer(String transfer_to) {
		((ExtendedInviteDialog) dialog).refer(new NameAddress(transfer_to));
	}

	// **************************** Logs ****************************/

}
